<?php

/////////////////////////////////////////////////////////////////////////////
// updateEdges.php - utility for updating edge data in edge table
// - meant for background processing purposes
//
// - FUTURE: - make it so that edge data is refreshed to a temp table,
//            then table renaming takes place.  i.e. perform these steps:
//
//            1) drop table temp_edge (if exists)
//            2) create table temp_edge (same structure as edge)
//            3) gather edge data
//            4) populate table temp_edge with edge data
//            5) "rename edge edge_old, temp_edge edge"
//            6) "drop table edge_old"
/////////////////////////////////////////////////////////////////////////////

require("config.inc.php");
require("database_manager.php");
require("leveltype.php");
require("node.php");
require("leaf.php");
require("edge.php");
require("company.php");

set_time_limit(0);

$edgeTablename = EDGE::$TABLE_EDGE;
$tmpEdgeTablename = "tmpedge";

/////////////////////////////////////////////////////////////////////////////
// Connect to the database.
DatabaseManager::getInstance(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE)->connect();

/////////////////////////////////////////////////////////////////////////////
// Drop and create temp_edge table.
DatabaseManager::getInstance()->query("DROP TABLE IF EXISTS " . Edge::$TEMP_TABLE_EDGE);
DatabaseManager::getInstance()->query(
    "CREATE TABLE " . Edge::$TEMP_TABLE_EDGE . "(" .
    "  id INT AUTO_INCREMENT PRIMARY KEY, " .
    "  sourcenode_id INT, " .
    "  targetnode_id INT, " .
    "  width INT, " .
    "  sourceleveltype_id INT, " .
    "  targetleveltype_id INT )"
);
DatabaseManager::getInstance()->query("ALTER TABLE " . Edge::$TEMP_TABLE_EDGE . " AUTO_INCREMENT = 1");


/////////////////////////////////////////////////////////////////////////////
// Get nodes by level
/////////////////////////////////////////////////////////////////////////////
$level1_nodes = Node::loadNodesOfLevelID(1);
print ("LEVEL 1 NODES found: " . count($level1_nodes) . "\n");
$level2_nodes = Node::loadNodesOfLevelID(2);
print ("LEVEL 2 NODES found: " . count($level2_nodes) . "\n");
$level3_nodes = Node::loadNodesOfLevelID(3);
print ("LEVEL 3 NODES found: " . count($level3_nodes) . "\n");
$level4_nodes = Node::loadNodesOfLevelID(4);
print ("LEVEL 4 NODES found: " . count($level4_nodes) . "\n");

/////////////////////////////////////////////////////////////////////////////
// Delete data from edge table (since it is to be replaced).
/////////////////////////////////////////////////////////////////////////////
/*
print("Deleting LEVEL 1 <--> LEVEL 2 edges... ");
$deletedRecords = Edge::deleteEdgeRecords(1, 2);
printf("%d edges deleted.\n", $deletedRecords);

print("Deleting LEVEL 2 <--> LEVEL 2 edges... ");
$deletedRecords = Edge::deleteEdgeRecords(2, 2);
printf("%d edges deleted.\n", $deletedRecords);

print("Deleting LEVEL 2 <--> LEVEL 3 edges... ");
$deletedRecords = Edge::deleteEdgeRecords(2, 3);
printf("%d edges deleted.\n", $deletedRecords);

print("Deleting LEVEL 3 <--> LEVEL 3 edges... ");
$deletedRecords = Edge::deleteEdgeRecords(3, 3);
printf("%d edges deleted.\n", $deletedRecords);

print("Deleting LEVEL 3 <--> LEVEL 4 edges... ");
$deletedRecords = Edge::deleteEdgeRecords(3, 4);
printf("%d edges deleted.\n", $deletedRecords);

print("Deleting LEVEL 4 <--> LEVEL 4 edges... ");
$deletedRecords = Edge::deleteEdgeRecords(4, 4);
printf("%d edges deleted.\n", $deletedRecords);
*/



/////////////////////////////////////////////////////////////////////////////
// Get edges by level relationship.
/////////////////////////////////////////////////////////////////////////////
// LEVEL 1 <--> LEVEL 2 edges:
$edges12 = Edge::getEdgesForNodes($level1_nodes, $level2_nodes);
print ("LEVEL 1 <--> LEVEL 2 edges: " . count($edges12) . "\n");
// print_r($edges12);

// LEVEL 2 <--> LEVEL 2 edges:
$edges22 = Edge::getEdgesForNodes($level2_nodes, $level2_nodes);
print ("LEVEL 2 <--> LEVEL 2 edges: " . count($edges22) . "\n");

// LEVEL 2 <--> LEVEL 3 edges:
$edges23 = Edge::getEdgesForNodes($level2_nodes, $level3_nodes);
print ("LEVEL 2 <--> LEVEL 3 edges: " . count($edges23) . "\n");

// LEVEL 3 <--> LEVEL 3 edges:
$edges33 = Edge::getEdgesForNodes($level3_nodes, $level3_nodes);
print ("LEVEL 3 <--> LEVEL 3 edges: " . count($edges33) . "\n");

// LEVEL 3 <--> LEVEL 4 edges:
$edges34 = Edge::getEdgesForNodes($level3_nodes, $level4_nodes);
print ("LEVEL 3 <--> LEVEL 4 edges: " . count($edges34) . "\n");

// LEVEL 4 <--> LEVEL 4 edges:
$edges44 = Edge::getEdgesForNodes($level4_nodes, $level4_nodes);
print ("LEVEL 4 <--> LEVEL 4 edges: " . count($edges44) . "\n");


/////////////////////////////////////////////////////////////////////////////
// Save edge data.
/////////////////////////////////////////////////////////////////////////////
print ("Saving LEVEL 1 <--> LEVEL 2 edges... ");
$result = Edge::bulkSaveEdgeRecords(1, 2, $edges12, Edge::$TEMP_TABLE_EDGE);
printf("%d edges inserted.  %d failed.\n", $result[0], $result[1]);

print ("Saving LEVEL 2 <--> LEVEL 2 edges... ");
$result = Edge::bulkSaveEdgeRecords(2, 2, $edges22, Edge::$TEMP_TABLE_EDGE);
printf("%d edges inserted.  %d failed.\n", $result[0], $result[1]);

print ("Saving LEVEL 2 <--> LEVEL 3 edges... ");
$result = Edge::bulkSaveEdgeRecords(2, 3, $edges23, Edge::$TEMP_TABLE_EDGE);
printf("%d edges inserted.  %d failed.\n", $result[0], $result[1]);

print ("Saving LEVEL 3 <--> LEVEL 3 edges... ");
$result = Edge::bulkSaveEdgeRecords(3, 3, $edges33, Edge::$TEMP_TABLE_EDGE);
printf("%d edges inserted.  %d failed.\n", $result[0], $result[1]);

print ("Saving LEVEL 3 <--> LEVEL 4 edges... ");
$result = Edge::bulkSaveEdgeRecords(3, 4, $edges34, Edge::$TEMP_TABLE_EDGE);
printf("%d edges inserted.  %d failed.\n", $result[0], $result[1]);

print ("Saving LEVEL 4 <--> LEVEL 4 edges... ");
$result = Edge::bulkSaveEdgeRecords(4, 4, $edges44, Edge::$TEMP_TABLE_EDGE);
printf("%d edges inserted.  %d failed.\n", $result[0], $result[1]);





/////////////////////////////////////////////////////////////////////////////
// Drop proper edge table and rename temp_edge table to edge.

DatabaseManager::getInstance()->query("DROP TABLE IF EXISTS " . Edge::$TABLE_EDGE);
DatabaseManager::getInstance()->query("ALTER TABLE " . Edge::$TEMP_TABLE_EDGE .
                                      " RENAME TO " . Edge::$TABLE_EDGE);


/////////////////////////////////////////////////////////////////////////////
// Disconnect from the database.
DatabaseManager::getInstance()->disconnect();

?>

